package com.qianguo.shop.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.shop.R;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.base.BaseTitleActivity;
import com.eva.androidlibrary.api.entity.ResultData;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * 找回密码第二步，重置密码
 *
 * @author eva
 * @date 16/3/9
 */
@EActivity(R.layout.activity_find_password_step2)
public class FindPasswordStep2Activity extends BaseTitleActivity {

    private static final String EXTRA_MOBILE = "mobile";
    private static final String EXTRA_AUTH_CODE = "auth_code";

    @ViewById(R.id.find_password_new_edit_text)
    MaterialEditText mEdtNewPsw;
    @ViewById(R.id.find_password_confirm_edit_text)
    MaterialEditText mEdtConfirmPsw;

    @Extra(EXTRA_MOBILE)
    String mMobile;
    @Extra(EXTRA_AUTH_CODE)
    String mAuthCode;

    public static void jumpToFindPswStep2(Activity activity, String mobile, String authCode) {
        Intent intent = new Intent(activity, com.qianguo.shop.activity.FindPasswordStep2Activity_.class);
        intent.putExtra(EXTRA_MOBILE, mobile);
        intent.putExtra(EXTRA_AUTH_CODE, authCode);
        activity.startActivityForResult(intent, 0);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.find_password_title);
    }

    @Override
    protected void initializeUI() {

    }

    /**
     * 确认修改密码
     */
    @Click(R.id.find_password_ok_button)
    void clickSubmit() {
        String newPsw = mEdtNewPsw.getText().toString();
        String confirmPsw = mEdtConfirmPsw.getText().toString();
        if (validateInfo(newPsw, confirmPsw)) {
            resetPassword(newPsw);
        }
    }

    /**
     * 后台请求重置密码
     *
     * @param password
     */
    @Background
    void resetPassword(String password) {
        mApp.getApi().resetPassword(mMobile, "1", mAuthCode, password, new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {
                showWarningDialog(FindPasswordStep2Activity.this, getString(R.string.hint), getString(R.string.find_password_success), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setResult(RESULT_OK);
                        finish();
                    }
                });
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(FindPasswordStep2Activity.this, msg);
            }
        });

    }

    /**
     * 验证密码的有效性
     *
     * @param newPsw
     * @param confirmPsw
     * @return
     */
    private boolean validateInfo(String newPsw, String confirmPsw) {
        if (newPsw.length() < 6 || newPsw.length() > 20) {
            mEdtNewPsw.setError(getString(R.string.edit_error_password));
            return false;
        }

        if (!newPsw.equals(confirmPsw)) {
            mEdtConfirmPsw.setError(getString(R.string.modify_password_error));
            return false;
        }

        return true;
    }
}
